package com.jcrj.service;

import com.jcrj.dao.MailDao;
import com.jcrj.entity.ODraftMail;
import com.jcrj.entity.OFile;
import com.jcrj.entity.OInMail;
import com.jcrj.entity.OMailLog;
import com.jcrj.entity.OMailReceiver;
import com.jcrj.entity.OOrg;
import com.jcrj.entity.OUser;
import com.jcrj.util.Base64;
import com.jcrj.util.FileUtil;
import com.jcrj.util.MailHtmlUtil;
import com.jcrj.util.SysConfig;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.tools.zip.ZipOutputStream;

public class MailService {
	private MailDao mailDao;
	private MessageService messageService;

	public MessageService getMessageService() {
		return this.messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public MailDao getMailDao() {
		return this.mailDao;
	}

	public void setMailDao(MailDao mailDao) {
		this.mailDao = mailDao;
	}

	public void saveInMail(OInMail mail) {
		this.mailDao.saveMail(mail);
	}

	public OUser getUserById(int id) {
		return (OUser) this.mailDao.getObject(new OUser(), id);
	}

	public void updateInMail(OInMail mail) {
		this.mailDao.updateInMail(mail);
	}

	public OInMail updateGetInMail(int id, OUser user) {
		OInMail mail = (OInMail) this.mailDao.getObject(new OInMail(), id);
		String hql = "from OMailReceiver o where o.inMail.id=" + mail.getId()
				+ " and o.receiverId=" + user.getId();
		List list = this.mailDao.findByHql(hql);
		if ((list != null) && (!list.isEmpty())) {
			OMailReceiver receiver = (OMailReceiver) list.get(0);
			if ("0".equals(receiver.getIfRead())) {
				receiver.setIfRead("1");
				this.mailDao.updateMailReceiver(receiver);
				OMailLog log = new OMailLog();
				log.setContent("已查阅");
				log.setReadTime(new Date());
				log.setUserId(user.getId());
				log.setName(user.getName());
				log.setMailId(Integer.valueOf(id));
				this.mailDao.saveMailLog(log);
				if ("1".equals(mail.getBack())) {
					OInMail inMail = new OInMail();
					inMail.setContent(Base64.encode(new String("该邮件已被"
							+ user.getName() + "拆封！").getBytes()));
					inMail.setTitle("Re:" + mail.getTitle());
					inMail.setCreateTime(new Date());
					inMail.setSendId(user.getId());
					inMail.setSendName(user.getName());
					inMail.setStatus("普通");
					this.mailDao.saveMail(inMail);
					OMailReceiver mailReceiver = new OMailReceiver();
					mailReceiver.setDel("0");
					mailReceiver.setIfRead("0");
					mailReceiver.setInMail(inMail);
					mailReceiver.setReceiverId(mail.getSendId());
					this.mailDao.saveMailReceiver(mailReceiver);
				}
			}
		}

		return mail;
	}

	public ODraftMail getDraftMail(int id) {
		return (ODraftMail) this.mailDao.getObject(new ODraftMail(), id);
	}

	public OMailReceiver getMailReceiver(int id, int userId) {
		String hql = "from OMailReceiver o where o.inMail.id=" + id
				+ " and o.receiverId=" + userId;
		List list = this.mailDao.findByHql(hql);
		OMailReceiver receiver = new OMailReceiver();
		if ((list != null) && (!list.isEmpty())) {
			receiver = (OMailReceiver) list.get(0);
		}
		return receiver;
	}

	public OInMail getInMailInfo(int id) {
		return (OInMail) this.mailDao.getObject(new OInMail(), id);
	}
	/**
	 * 
	 * 
	 * @param mail
	 * @param user
	 * @param reply
	 */
	public void saveInMail(ODraftMail mail, OUser user, String reply) {
		OInMail inMail = new OInMail();
		inMail.setBack(mail.getBack());
		inMail.setCNames(mail.getCNames());
		if ((mail.getContent() != null) && (!"".equals(mail.getContent()))) {
			inMail.setContent(Base64.encode(mail.getContent().getBytes()));
		}
		inMail.setCreateTime(new Date());
		inMail.setFileId(mail.getFileId());
		inMail.setReceiverName(mail.getReceiverNames());
		inMail.setTitle(mail.getTitle());
		inMail.setStatus(mail.getStatus());
		inMail.setSendId(user.getId());
		inMail.setSendName(user.getName());
		inMail.setDel("0");
		inMail.setMsg(mail.getMsg());
		inMail.setFileSize(Long.valueOf(getAllFileSize(mail.getFileId())));
		this.mailDao.saveMail(inMail);
		Map map = new HashMap();
		String[] array = mail.getReceiverIds().split(",");
		String userIds = "";
		for (int i = 0; i < array.length; i++) {
			if (array[i].indexOf("org") != -1) {
				OOrg org = (OOrg) this.mailDao.getObject(new OOrg(),
						Integer.parseInt(array[i].split(":")[0]));
				if (org.getPid().intValue() == 0) {
					userIds = "all";
					break;
				}
				map = getUsersByOrg(org.getId().intValue());
			} else {
				map.put(Integer.valueOf(Integer.parseInt(array[i])),
						getUserById(Integer.parseInt(array[i])));
			}
		}

		if ((!"all".equals(userIds)) && (mail.getCIds() != null)
				&& (!"".equals(mail.getCIds()))) {
			String[] ids = mail.getCIds().split(",");
			for (int i = 0; i < ids.length; i++) {
				if (ids[i].indexOf("org") != -1) {
					OOrg org = (OOrg) this.mailDao.getObject(new OOrg(),
							Integer.parseInt(ids[i].split(":")[0]));
					if (org.getPid().intValue() == 0) {
						userIds = "all";
						break;
					}
					map = getUsersByOrg(org.getId().intValue());
				} else {
					map.put(Integer.valueOf(Integer.parseInt(ids[i])),
							getUserById(Integer.parseInt(ids[i])));
				}
			}
		}

		if ((!"all".equals(userIds)) && (mail.getMIds() != null)
				&& (!"".equals(mail.getMIds()))) {
			String[] ids = mail.getMIds().split(",");
			for (int i = 0; i < ids.length; i++) {
				if (ids[i].indexOf("org") != -1) {
					OOrg org = (OOrg) this.mailDao.getObject(new OOrg(),
							Integer.parseInt(ids[i].split(":")[0]));
					if (org.getPid().intValue() == 0) {
						userIds = "all";
						break;
					}
					map = getUsersByOrg(org.getId().intValue());
				} else {
					map.put(Integer.valueOf(Integer.parseInt(ids[i])),
							getUserById(Integer.parseInt(ids[i])));
				}
			}
		}

		if ("all".equals(userIds)) {
			String hql = "from OUser where del='0'";
			List<OUser> list = this.mailDao.findByHql(hql);
			for (OUser u : list) {
				OMailReceiver receiver = new OMailReceiver();
				receiver.setDel("0");
				receiver.setIfRead("0");
				receiver.setInMail(inMail);
				receiver.setReceiverId(u.getId());
				this.mailDao.saveMailReceiver(receiver);
				if ((!"1".equals(inMail.getMsg())) || (u.getPhone() == null)
						|| ("".equals(u.getPhone())))
					continue;
				this.messageService.send(u.getPhone(),
						"您收到了来自" + user.getName() + "的新邮件" + inMail.getTitle()
								+ "，请尽快处理！");
			}
		} else {
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				Integer key = (Integer) it.next();
				OUser u = (OUser) map.get(key);
				OMailReceiver receiver = new OMailReceiver();
				receiver.setDel("0");
				receiver.setIfRead("0");
				receiver.setInMail(inMail);
				receiver.setReceiverId(key);
				this.mailDao.saveMailReceiver(receiver);
				if ((!"1".equals(inMail.getMsg())) || (u.getPhone() == null)
						|| ("".equals(u.getPhone())))
					continue;
				this.messageService.send(u.getPhone(),
						"您收到了来自" + user.getName() + "的新邮件" + inMail.getTitle()
								+ "，请尽快处理！");
			}

		}

		if ((reply != null) && (!"".equals(reply))) {
			String sql = "update O_Mail_Receiver set if_Read='2' where mail_id="
					+ reply + " and receiver_Id=" + user.getId();
			this.mailDao.executeSql(sql);
		}
	}

	private Map<Integer, OUser> getUsersByOrg(int orgId) {
		Map map = new HashMap();
		String sql = "select id,name from o_user where org_id in(SELECT LPAD(' ',2*(LEVEL - 1), ' ')||id AS id FROM o_org CONNECT BY pid = PRIOR id  START WITH id ="
				+ orgId + ")";
		List<Object[]> list = this.mailDao.findBySql(sql);
		for (Object[] obj : list) {
			OUser user = new OUser();
			user.setId(Integer.valueOf(Integer.parseInt(obj[0].toString())));
			user.setName(obj.toString());
			map.put(user.getId(), user);
		}
		return map;
	}

	public void saveSendInMail(String title, String conent, String actorId,
			String actor, OUser user) {
		OInMail mail = new OInMail();
		mail.setCreateTime(new Date());
		mail.setDel("0");
		mail.setSendId(user.getId());
		mail.setStatus("普通");
		mail.setSendName(user.getName());
		mail.setTitle(title);
		mail.setContent(Base64.encode(conent.getBytes()));
		mail.setReceiverName(actor);
		this.mailDao.saveMail(mail);
		if ((user != null) && (!"".equals(user))) {
			OMailReceiver receiver = new OMailReceiver();
			receiver.setDel("0");
			receiver.setIfRead("0");
			receiver.setInMail(mail);
			receiver.setReceiverId(Integer.valueOf(Integer.parseInt(actorId)));
			this.mailDao.saveMailReceiver(receiver);
		}
	}

	public void saveDraftMail(ODraftMail mail) {
		if (mail.getStatus() == null) {
			mail.setStatus("普通");
		}
		this.mailDao.saveDraftMail(mail);
	}

	public void deleteDraftMail(ODraftMail mail) {
		this.mailDao.deleteDraftMail(mail);
	}

	public List<ODraftMail> getDraftMailList(String title, String startDate,
			String endDate, int userId, int currentPage) {
		String hql = "from ODraftMail o where o.sendId=" + userId + " ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.title like '%" + title + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by o.createTime desc,o.id desc";
		return this.mailDao.findByPage(hql, currentPage);
	}

	public Long getDraftMailCount(String title, String startDate,
			String endDate, int userId) {
		String hql = "select count(id) from ODraftMail o where o.sendId="
				+ userId + " ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.title like '%" + title + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.mailDao.countTotalByHql(hql);
	}

	public List<OMailReceiver> getInMailList(String title, String name,
			String startDate, String endDate, String userId, int currentPage) {
		String hql = " from OMailReceiver o where o.del='0' and o.receiverId in("
				+ userId + ") ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.inMail.title like '%" + title.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.inMail.sendName like '%" + name.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.inMail.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.inMail.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by o.inMail.createTime desc, o.ifRead asc";
		return this.mailDao.findByPage(hql, currentPage);
	}

	public Long getHasNotReadMail(OUser user) {
		String hql = "select count(o.id) from OMailReceiver o where o.del='0' and o.ifRead='0' and o.receiverId="
				+ user.getId();
		return this.mailDao.countTotalByHql(hql);
	}

	public Long getInMailCount(String title, String name, String startDate,
			String endDate, String userId) {
		String hql = "select count(o.id) from OMailReceiver o where o.del='0' and o.receiverId in("
				+ userId + ") ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.inMail.title like '%" + title.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.inMail.sendName like '%" + name.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.inMail.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.inMail.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.mailDao.countTotalByHql(hql);
	}

	public void updateInMail(String[] ids, OUser user) {
		if ((ids != null) && (ids.length > 0)) {
			String mailId = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(mailId))
					mailId = mailId + ids[i];
				else {
					mailId = mailId + "," + ids[i];
				}
			}
			String hql = "update OMailReceiver o set o.del='1' where o.inMail.id in("
					+ mailId + ") and o.receiverId=" + user.getId();
			this.mailDao.executeHql(hql);
		}
	}

	public void deleteInMail(String[] ids, OUser user) {
		if ((ids != null) && (ids.length > 0)) {
			String mailId = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(mailId))
					mailId = mailId + ids[i];
				else {
					mailId = mailId + "," + ids[i];
				}
			}
			String hql = "delete from OMailReceiver o where o.inMail.id in("
					+ mailId + ") and o.receiverId=" + user.getId();
			this.mailDao.executeHql(hql);
		}
	}

	public void deleteOuttMail(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String mailId = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(mailId))
					mailId = mailId + ids[i];
				else {
					mailId = mailId + "," + ids[i];
				}
			}
			String hql = "update OInMail o set o.del='1'  where o.id in("
					+ mailId + ")";
			this.mailDao.executeHql(hql);
		}
	}

	public void deleteDraftMail(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String mailId = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(mailId))
					mailId = mailId + ids[i];
				else {
					mailId = mailId + "," + ids[i];
				}
			}
			String hql = "delete from ODraftMail o where o.id in(" + mailId
					+ ")";
			this.mailDao.executeHql(hql);
		}
	}

	public List<OInMail> getSendMailList(String title, String name,
			String startDate, String endDate, int userId, int currentPage) {
		String hql = " from OInMail o where o.del='0' and   o.sendId=" + userId
				+ " ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.title like '%" + title + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.receiverName like '%" + name.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by o.createTime desc,o.id desc";
		return this.mailDao.findByPage(hql, currentPage);
	}

	public Long getSendMailCount(String title, String name, String startDate,
			String endDate, int userId) {
		String hql = "select count(o.id) from OInMail o where o.del='0' and o.sendId="
				+ userId + " ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.title like '%" + title.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.receiverName like '%" + name.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.mailDao.countTotalByHql(hql);
	}

	public List<OMailReceiver> getDeleteMailList(String title,
			String startDate, String endDate, int userId, int currentPage) {
		String hql = " from OMailReceiver o where o.del='1' and o.receiverId="
				+ userId + " ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.inMail.title like '%" + title + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by o.ifRead,o.inMail.createTime desc,o.id desc";
		return this.mailDao.findByPage(hql, currentPage);
	}

	public Long getDeleteMailCount(String title, String startDate,
			String endDate, int userId) {
		String hql = "select count(o.id) from OMailReceiver o where o.del='1' and o.receiverId="
				+ userId + " ";
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.inMail.title like '%" + title + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + startDate
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endDate
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.mailDao.countTotalByHql(hql);
	}

	public List<OMailLog> readUserList(int mailId) {
		String hql = "from OMailLog where mailId=" + mailId;
		return this.mailDao.findByHql(hql);
	}

	public List<Integer> getUserList(List<OMailLog> list) {
		List idlist = new ArrayList();
		if ((list != null) && (!list.isEmpty())) {
			for (OMailLog log : list) {
				idlist.add(log.getUserId());
			}
		}
		return idlist;
	}

	public List<String> notReadUserList(List list, int mailId) {
		if ((list != null) && (!list.isEmpty())) {
			String hql = "select name from OUser where id in(select receiverId from OMailReceiver o where o.inMail.id in (:mailId) and o.receiverId not in (:list))";
			return this.mailDao.findListByName(hql, new String[] { "mailId",
					"list" }, new Object[] { Integer.valueOf(mailId), list });
		}
		String hql = "select name from OUser where id in(select receiverId from OMailReceiver o where o.inMail.id in (:mailId))";
		return this.mailDao.findListByName(hql, new String[] { "mailId" },
				new Object[] { Integer.valueOf(mailId) });
	}

	public File getMailAllList(String ids) {
		String url = SysConfig.getInstance().getProperty("filepath")
				+ "/mail.rar";
		File sumFile = new File(url);
		ZipOutputStream zip = null;
		MailHtmlUtil util = new MailHtmlUtil();
		List files = new ArrayList();
		List names = new ArrayList();
		Map map = new HashMap();
		if ((ids != null) && (!"".equals(ids))) {
			try {
				String hql = " from OInMail where id in(" + ids + ")";
				List<OInMail> list = this.mailDao.findByHql(hql);
				for (OInMail mail : list) {
					File mailDoc = util.getMailFile(mail);
					map = getFileList(mail.getFileId(), mail.getId());
					files.add(mailDoc);
					names.add(mail.getTitle() + mailDoc.getName());
					if ((map != null) && (!map.isEmpty())) {
						files.addAll((Collection) map.get("file"));
						names.addAll((List) map.get("fileName"));
					}
				}

				zip = new ZipOutputStream(new FileOutputStream(url));
				FileUtil.zipFile(files, names, zip);
				zip.flush();
			} catch (Exception e) {
				e.printStackTrace();
				try {
					if (zip != null)
						zip.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			} finally {
				try {
					if (zip != null)
						zip.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sumFile;
	}

	public Map getFileList(String files, Integer mailId) {
		Map map = new HashMap();
		List listFile = new ArrayList();
		List fileName = new ArrayList();
		if ((files != null) && (!"".equals(files))) {
			String[] fileArray = files.split("_");
			String file = "";
			for (int i = 0; i < fileArray.length; i++) {
				if ("".equals(file))
					file = file + fileArray[i];
				else {
					file = file + "," + fileArray[i];
				}
			}
			String hql = "from OFile o where o.id in(" + file + ")";
			List<OFile> list = this.mailDao.findByHql(hql);
			for (OFile o : list) {
				String path1 = o.getName().substring(0,
						o.getName().lastIndexOf("."));
				String type = o.getName().substring(
						o.getName().lastIndexOf("."));
				String newFile = path1 + mailId + type;
				fileName.add(newFile);
				listFile.add(new File(o.getUrl()));
			}
			map.put("file", listFile);
			map.put("fileName", fileName);
		}
		return map;
	}

	public long getAllFileSize(String fileId) {
		long size = 0L;
		if ((fileId != null) && (!"".equals(fileId))) {
			String[] files = fileId.split("_");
			for (int i = 0; i < files.length; i++) {
				if ((files[i] != null) && (!"".equals(files[i]))) {
					OFile file = (OFile) this.mailDao.getObject(new OFile(),
							Integer.parseInt(files[i]));
					size += file.getFileSize().longValue();
				}
			}
		}
		return size;
	}

	public String countMailVolume(int userId) {
		String total = "0";
		String sql = " select sum(t.file_size) from o_in_mail t where t.id in(select r.mail_id from o_mail_receiver r where r.receiver_id ="
				+ userId + ")";
		BigDecimal volume = this.mailDao.countBySql(sql);
		if ((volume != null) && (!"".equals(volume))) {
			double sum = volume.doubleValue() / 1024.0D / 1024.0D;
			BigDecimal m = new BigDecimal(sum);
			total = m.setScale(2, 4).toString();
		}
		return total;
	}

	public Integer getMailNextOrUpId(int userId, int mailId, String statu) {
		String hql = "";
		Integer ids = null;
		if ("next".equals(statu))
			hql = "from OMailReceiver o where o.inMail.createTime<(select m.inMail.createTime from OMailReceiver m where m.receiverId="
					+ userId
					+ " and m.inMail.id="
					+ mailId
					+ ") and o.receiverId="
					+ userId
					+ " and o.del='0' order by o.inMail.createTime desc";
		else {
			hql = "from OMailReceiver o where o.inMail.createTime>(select m.inMail.createTime from OMailReceiver m where m.receiverId="
					+ userId
					+ " and m.inMail.id="
					+ mailId
					+ ") and o.receiverId="
					+ userId
					+ " and o.del='0' order by o.inMail.createTime asc";
		}
		List list = this.mailDao.findListByHqlTop(hql, 1);
		if ((list != null) && (!list.isEmpty())) {
			OMailReceiver mail = (OMailReceiver) list.get(0);
			ids = mail.getInMail().getId();
		}
		return ids;
	}

	public boolean addMailRollBck(int mailId) {
		boolean flag = true;
		String hql = "from OMailReceiver o where o.inMail.id=" + mailId;
		List<OMailReceiver> list = this.mailDao.findByHql(hql);
		for (OMailReceiver receive : list) {
			if ("1".equals(receive.getIfRead())) {
				flag = false;
				return flag;
			}
		}
		if (flag) {
			String receHql = "delete from OMailReceiver o where o.inMail.id="
					+ mailId;
			this.mailDao.executeHql(receHql);
			OInMail mail = getInMailInfo(mailId);
			ODraftMail draftMail = new ODraftMail();
			draftMail.setTitle(mail.getTitle());
			draftMail.setStatus(mail.getStatus());
			if ((mail.getContent() != null) && (!"".equals(mail.getContent()))) {
				draftMail.setContent(Base64.decode(mail.getContent()));
			}
			draftMail.setSendId(mail.getSendId());
			draftMail.setSendName(mail.getSendName());
			draftMail.setCreateTime(mail.getCreateTime());
			draftMail.setFileId(mail.getFileId());
			this.mailDao.saveDraftMail(draftMail);
			this.mailDao.deleteMail(mail);
		}
		return flag;
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.service.MailService JD-Core Version: 0.6.0
 */